package genetic.q2;

import java.util.Random;

public class board {

	//empty=0	wall=1	can=2
	private int[] loc = new int[2];
	private int[][] _board = new int[12][12];
	private Random r = new Random();

	public board(){
		startBoard();
	}

	public double moved(int move){
		double score = 0;
		switch(move){
		case 0:		//move north
			switch(_board[loc[0]-1][loc[1]]){
			case 1:
				score = -5;
				break;
			default:
				loc[0] = loc[0]-1;
				loc[1] = loc[1];
				break;
			}

			break;
		case 1:		//move south
			switch(_board[loc[0]+1][loc[1]]){
			case 1:
				score = -5;
				break;
			default:
				loc[0] = loc[0]+1;
				loc[1] = loc[1];
				break;
			}
			break;
		case 2:		//move east
			switch(_board[loc[0]][loc[1]+1]){
			case 1:
				score = -5;
				break;
			default:
				loc[0] = loc[0];
				loc[1] = loc[1]+1;
				break;
			}

			break;
		case 3:		//move west
			switch(_board[loc[0]][loc[1]-1]){
			case 1:
				score = -5;
				break;
			default:
				loc[0] = loc[0];
				loc[1] = loc[1]-1;
				break;
			}

			break;
		case 4:		//move random
			score = moved(r.nextInt(4));
			break;
		case 6:		//pick-up can
			switch(_board[loc[0]][loc[1]]){
			case 0:
				score = -1;
				break;
			case 2:
				score = 10;
				_board[loc[0]][loc[1]]=0;
				break;
			}
			break;
		}
		return score;
	}

	public int[] look(){
		int[] ans = new int[5];
		ans[0]=_board[loc[0]][loc[1]];	//current position
		ans[1]=_board[loc[0]-1][loc[1]];	//north
		ans[2]=_board[loc[0]+1][loc[1]];	//south
		ans[3]=_board[loc[0]][loc[1]+1];	//east
		ans[4]=_board[loc[0]][loc[1]-1];	//west
		return ans;
	}

	public int startBoard(){
		loc[0]=1;
		loc[1]=1;
		for(int i=0; i<12;i++){
			if(i==0){
				for(int j=0; j<12;j++){
					_board[i][j]=1;
				}
			}
			else if(i==11){
				for(int j=0; j<12;j++){
					_board[i][j]=1;
				}
			}
			else{
					_board[i][0]=1;
					for(int j=1; j<11;j++){
						_board[i][j]=0;
					}
					_board[i][11]=1;
			}
		}
		int cans = r.nextInt(100);
		for(int j=0; j<cans;j++){
			int row = r.nextInt(10);
			int col = r.nextInt(10);
			_board[row+1][col+1]=2;
		}
		int counter = 0;
		for(int j=0; j<12;j++){
			for(int i=0; i<12;i++){
				if (_board[i][j]==2)
					counter=counter+10;
			}
		}
		return counter;
	}
	
	public int check(int move){
		int score = 5;
		switch(move){
		case 0:		//move north
			switch(_board[loc[0]-1][loc[1]]){
			case 1:
				score = 0;
				break;
			}

			break;
		case 1:		//move south
			switch(_board[loc[0]+1][loc[1]]){
			case 1:
				score = 0;
				break;
			}
			break;
		case 2:		//move east
			switch(_board[loc[0]][loc[1]+1]){
			case 1:
				score = 0;
				break;
			}
			break;
		case 3:		//move west
			switch(_board[loc[0]][loc[1]-1]){
			case 1:
				score = 0;
				break;
			}
			break;
		case 4:		//move random
			score = check(r.nextInt(4));
			break;
		case 6:		//pick-up can
			switch(_board[loc[0]][loc[1]]){
			case 0:
				score = 4;
				break;
			case 2:
				score = 15;
				break;
			}
			break;
		}
		return score;
	}
}
